package com.ly.common.base.query;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.data.jpa.domain.Specification;

import java.io.Serializable;

/**
 * 公共查询分页实体
 * @Auther: lzw
 * @Date: 2018/6/4
 */
public abstract class BaseQuery implements Serializable {
    @JSONField(serialize=false)
    private Integer pageIndex= 1;//当前页码
    @JSONField(serialize=false)
    private Integer pageSize = 20; //页面大小，默认20
    @JSONField(serialize=false)
    private Integer pageIndexOffset= 0;//当前页码偏移量
    @JSONField(serialize=false)
    private Integer offset = 0; // 行偏移
    @JSONField(serialize=false)
    private Integer limit;    //获取最大数量
    @JSONField(serialize=false)
    private String orders="";// 排序字段 例如: id asc,pCode desc
    @JSONField(serialize=false)
    private Boolean isDesc = true;// 是否倒序，默认是
    /*排序字段第一分隔符*/
    final private  String ordersSeparator1=",";
    /*排序字段第二分隔符*/
    final private  String ordersSeparator2=" ";

    public Integer getPageIndex() {
        return pageIndex;
    }
    public void setPageIndex(Integer pageIndex) {
        if(pageIndex < 1){
            pageIndex=1;
        }
        this.pageIndex = pageIndex;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Integer getOffset() {
        return (this.getPageIndex() - 1) * this.getPageSize();
    }
    public void setOffset(Integer offset) {
        this.offset = offset;
    }
    public Integer getLimit() {
        return limit;
    }
    public void setLimit(Integer limit) {
        if (limit > 500)
            limit = 500;
        this.limit = limit;
    }
    public String getOrders() {
        return orders;
    }
    public void setOrders(String orders) {
        this.orders = orders;
    }
    public Boolean getIsDesc() {
        return isDesc;
    }
    public void setIsDesc(Boolean isDesc) {
        this.isDesc = isDesc;
    }

    public Integer getPageIndexOffset() {
        return this.getPageIndex() - 1;
    }

    public void setPageIndexOffset(Integer pageIndexOffset) {
        this.pageIndexOffset = pageIndexOffset;
    }

    public String getOrdersSeparator1() {
        return ordersSeparator1;
    }

    public String getOrdersSeparator2() {
        return ordersSeparator2;
    }

    public abstract Specification toSpecification();
}
